<template>
  <div class="recude">
    <pre>
      Array.prototype.my_reduce = function (callback, initialValue) {
        <!-- reduce() 对于空数组是不会执行回调函数的。 -->
        if (!Array.isArray(this) || !this.length || typeof callback !== 'function') {
          return []
        } else {
          // 判断是否有初始值
          let hasInitialValue = initialValue !== undefined;
          let value = hasInitialValue ? initialValue : tihs[0];
          for (let index = hasInitialValue ? 0 : 1; index < this.length; index++) {
            const element = this[index];
            value = callback(value, element, index, this)
          }
          return value
        }
      }

      let arr = [1, 2, 3, 4, 5]
      let res = arr.my_reduce((pre, cur, i, arr) => {
        console.log(pre, cur, i, arr)
        return pre + cur
      }, 10)
      console.log(res)//25
    </pre>
  </div>
</template>
 
<script>
export default {
  name: 'reduce',
  data () {
    return {
      msg:''
    }
  }
}
</script>
 
<style scoped lang="scss">
 
</style>